package com.example.backend.controller;

import com.example.backend.entity.DingDan;
import com.example.backend.entity.DingDanDetail;
import com.example.backend.entity.DingDanMessage;
import com.example.backend.service.DingDanService;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


// 用 HTTP 方法 （GET-查、POST-增、PUT-改、DELETE-删） 表示对资源的操作。
@RestController
@CrossOrigin
@Log4j
public class DingDanController {
    @Autowired
    private DingDanService dingDanService;

    /** 添加订单 */
    @PostMapping("/dingdan")
    public int doAddDingDan(@RequestBody DingDan dingDan) {
        return dingDanService.addDingDan(dingDan);
    }
    /** 修改订单 */
    @PutMapping("/dingdan")
    public int doUpdateDingDan(@RequestBody DingDan dingDan) {
        return dingDanService.updateDingDan(dingDan);
    }
    /** 删除订单 */
    @DeleteMapping("/dingdan")
    public int doDeleteDingDan(@RequestBody String id) {
        return dingDanService.deleteDingDanById(id);
    }
    /** 通过Id查询订单 */
    @GetMapping("/dingdan/{id}")
    public DingDan doSelectDingDanById(@PathVariable String id) {
        return dingDanService.selectDingDanById(id);
    }
    /** 查询订单 */
    @GetMapping("/dingdan")
    public Map<String, Object> doSelectDingDans(DingDan dingDan,
                                @RequestParam(defaultValue = "1") int pageNum,
                                @RequestParam(defaultValue = "5") int pageSize) {
        return dingDanService.selectDingDans(dingDan, pageNum, pageSize);
    }


    /** 订单编号查询订单 */
    @GetMapping("/dingdan/dingdan_num/{dingdan_num}")
    public DingDan doSelectDingDanByDingDanNum(@PathVariable String dingdan_num) {
        return dingDanService.selectDingDanByDingDanNum(dingdan_num);
    }
    @GetMapping("/dingdan/score")
    public DingDan  doSelectDingDans(DingDan dingDan) {
        return dingDanService.selectDingDans(dingDan).get(0);
    }

    @GetMapping("/dingdan/nopage")
    public ArrayList<DingDanMessage>  doSelectDingDansNoPage(DingDan dingDan) {
        return dingDanService.selectDingDansNotPage(dingDan);
    }

    /**
     * 根据订单编号获取订单详情（菜品信息）
     */
    @GetMapping("/dingdan/detail/{dingdan_num}")
    public List<DingDanDetail> getDingDanDetails(@PathVariable String dingdan_num) {
        return dingDanService.getDingDanDetailsByDingDanNum(dingdan_num);
    }

}
